package com.homegoods.resource.util;

import android.support.annotation.ColorRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.ImageViewCompat;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kedll.library.widget.pullDownMenu.OnDisplayClickListener;
import com.kedll.library.widget.pullDownMenu.PullDownMenuView;
import com.kedll.library.widget.pullDownMenu.PullDownMenuView.PullDownMenuViewAdapter;

/**
 * @author QiQi
 */
public class MultiPullDownMenuProxy {
    /**
     * 当前选择的菜单项
     */
    private int currentCondition;
    private SparseArray<ViewGroup> menuArray = new SparseArray<>();
    private PullDownMenuView pullDownMenu;

    @ColorRes
    private int selectColor;

    @ColorRes
    private int unSelectColor;

    public MultiPullDownMenuProxy(PullDownMenuView pullDownMenu, int selectColor, int unSelectColor) {
        this.pullDownMenu = pullDownMenu;
        this.selectColor = selectColor;
        this.unSelectColor = unSelectColor;

        pullDownMenu.setOnDisplayClickListener(new OnDisplayClickListener() {
            @Override
            public void onExpand(View view) {
                rotateDropIconAnimator(180, view);
            }

            @Override
            public void onCollapse(View view) {
                rotateDropIconAnimator(0, view);
            }
        });
    }

    private void rotateDropIconAnimator(int degree, View view) {
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            if (group.getChildCount() > 1) {
                View subView = group.getChildAt(1);
                if (subView instanceof ImageView) {
                    subView.animate().rotation(degree).setDuration(300L).start();
                    if (degree == 0) {
                        switchSelector(group, unSelectColor);
                    } else {
                        switchSelector(group, selectColor);
                    }
                }
            }
        }
    }

    private void switchSelector(ViewGroup group, @ColorRes int color) {
        TextView view = (TextView) group.getChildAt(0);
        ImageView indicator = (ImageView) group.getChildAt(1);
        switchSelector(view, indicator, color);
    }

    /**
     * 切换条件选择状态
     *
     * @param view
     * @param indicator
     * @param color
     */
    private void switchSelector(TextView view, ImageView indicator, @ColorRes int color) {
        view.setTextColor(ContextCompat.getColor(view.getContext(), color));
        ImageViewCompat.setImageTintList(indicator, ContextCompat.getColorStateList(view.getContext(), color));
    }

    public void registerMenu(int key, ViewGroup area) {
        menuArray.put(key, area);
    }

    private boolean isExpand() {
        return pullDownMenu.isExpand();
    }

    /**
     * 切换下拉菜单栏绑定的对象
     *
     * @param adapter
     * @param view
     */
    public void switchAttachView(PullDownMenuViewAdapter adapter, View view) {
        if (isExpand()) {
            pullDownMenu.switchDisplay(pullDownMenu.getAttachView());
            if (view == pullDownMenu.getAttachView()) {
                return;
            }
        }
        pullDownMenu.attachControlView(view);
        pullDownMenu.setAdapter(adapter);
        pullDownMenu.switchDisplay(view);
    }

    private int getCurrentCondition() {
        return currentCondition;
    }

    public void setCurrentCondition(int currentCondition) {
        if (currentCondition == getCurrentCondition()) {
            return;
        }
        this.currentCondition = currentCondition;
    }
}